﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLNhaHangTiecCuoi.Models;
using System.Threading.Tasks;

namespace QLNhaHangTiecCuoi.Controllers
{
    public class ThucDonsController : Controller
    {
        private QLNhaHangTCEntities db = new QLNhaHangTCEntities();

        // GET: /ThucDons/
        public async Task<ActionResult> Index()
        {
            return View(await db.ThucDons.ToListAsync());
        }

        // GET: /ThucDons/Details/5
        public async Task<ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ThucDon ThucDon = await db.ThucDons.FindAsync(id);
            if (ThucDon == null)
            {
                return HttpNotFound();
            }
            return View(ThucDon);
        }

        // GET: /ThucDons/Create
        public ActionResult Create()
        {
            return View();
        }
        public string CreateMaTD()
        {
            var item = db.ThucDons.OrderByDescending(i => i.MaTD).FirstOrDefault();
            string ma;
            if (item == null) { ma = "TD0000"; }
            else ma = item.MaTD;
            int so = Convert.ToInt32(ma.Substring(2, 4));
            so++;
            string MaTD = "TD" + so.ToString("0000");
            return MaTD;
        }
        // POST: /ThucDons/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "TenTD,MoTa,HinhAnh")] ThucDon ThucDon)
        {
            ThucDon thucdon = new ThucDon()
            {
                MaTD=CreateMaTD(),
                TenTD=ThucDon.TenTD,
                MoTa = ThucDon.MoTa,
                HinhAnh=ThucDon.HinhAnh
            };

            if (ModelState.IsValid)
            {
                db.ThucDons.Add(thucdon);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(ThucDon);
        }

        // GET: /ThucDons/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ThucDon thucdon = await db.ThucDons.FindAsync(id);
            if (thucdon == null)
            {
                return HttpNotFound();
            }
            return View(thucdon);
        }

        // POST: /ThucDons/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "MaTD,TenTD,MoTa,HinhAnh")] ThucDon ThucDon)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ThucDon).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(ThucDon);
        }

        // GET: /ThucDons/Delete/5
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ThucDon thucdon = await db.ThucDons.FindAsync(id);
            if (thucdon == null)
            {
                return HttpNotFound();
            }
            return View(thucdon);
        }

        // POST: /ThucDons/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            ThucDon thucdon = await db.ThucDons.FindAsync(id);
            db.ThucDons.Remove(thucdon);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
